package com.haozhen

/**
  * @author haozhen
  * @email haozh@ync1.com
  * @date 2020/12/27  14:45
  */
object UDFDemo {

  def main(args: Array[String]): Unit = {
    import org.apache.spark.sql.SparkSession

    val spark = SparkSession.builder().master("local")
      .appName("udfdemo").getOrCreate()
    spark.sparkContext.setLogLevel("WARN")
    import spark.implicits._
    import  spark.sql

    val data = List(("scala","author1"),("spark","author2"),
                    ("hadoop","author3"),("hive","author4"),
                    ("strom","author5"),("kafka","author6"))

    val df = data.toDF("title","author")
    df.createTempView("books")

    def len1(str:String):Int = str.length

    spark.udf.register("len1",len1 _)

    sql("select title,author,len1(title) from books").show
    sql("select title,author from books where len1(title)>4").show

    //DSL

    df.filter("len1(title)>5").show


    import org.apache.spark.sql.functions._

    val len2 = udf(len1 _)

    df.select($"title",$"author",len2($"title")).show

  }

}
